package com.haois.mp.database.module.offaccounts.controller;


import com.google.gson.Gson;
import com.haois.mp.database.exception.ErrorException;
import com.haois.mp.database.exception.OutException;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.haois.mp.database.module.basic.controller.BaseController;
import com.haois.mp.database.commom.GetModelAndView;
import com.haois.mp.database.commom.constant.StatusCode;
import com.haois.mp.database.module.remote.service.INoticeService;
import com.haois.mp.database.module.remote.service.IUserService;
import com.haois.mp.database.module.wxserver.service.TempMessageService;
import com.haois.mp.database.util.mp.DataCheck;
import com.haois.mp.database.util.mp.ProduceJwt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/wxmp/share")
public class ShareController extends BaseController {

    private static final Logger logger = LoggerFactory
            .getLogger(ShareController.class);

    @Autowired
    private IUserService iUserService;
    @Autowired
    private INoticeService iNoticeService;
    @Autowired
    private TempMessageService tempMsgService;

    private ProduceJwt produceJwt = new ProduceJwt();

    private Gson gson = new Gson();

    /**
     * 前端获取url签名
     *
     * @throws WxErrorException
     */
    @RequestMapping("/getJsApiSignature")
    @ResponseBody
    public Map getJsApiSignature(String url)
            throws RuntimeException, WxErrorException {
        WxJsapiSignature wxJsapiSignature = wxMpService.createJsapiSignature(url);
        Map map = new HashMap();
        map.put("wxConfig", wxJsapiSignature);
        return map;
    }


    @RequestMapping("/member/{memberNum:.+}")
    public ModelAndView shareMember(HttpServletRequest request,
                                    @PathVariable String memberNum)
            throws RuntimeException, WxErrorException, ErrorException, OutException {
        ModelAndView mv = new ModelAndView("/share/member");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        String access_token = produceJwt.getShareToken();
        Map result = iUserService.getUserInfo(access_token, memberNum, StatusCode.SHARE_OID);
        DataCheck.codeNot0X(result);
        mv.addObject("result", gson.toJson(result));
        mv.addObject("memberNum", memberNum);
        return mv;
    }

    @RequestMapping("/group/{oid}")
    public ModelAndView detail(HttpServletRequest request, @PathVariable String oid
    ) throws RuntimeException, WxErrorException, ErrorException {

        ModelAndView mv = new ModelAndView("/share/group");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("oid", oid);
        return mv;
    }

    @RequestMapping(value = "/notice/{id}")
    public ModelAndView getById(@PathVariable Integer id) throws WxErrorException, OutException {
        String access_token = produceJwt.getShareToken();
        Map result = iNoticeService.getNoticeById(access_token, id);
        ModelAndView mv = new ModelAndView("/share/notice");
        if (!"00".equals(result.get("code"))) {
            mv = GetModelAndView.get404Page();
        }
        mv.addObject("result", gson.toJson(result));
        return mv;
    }
    @RequestMapping(value = "/tempMsg/{id}")
    public ModelAndView getTempById(@PathVariable String id) throws WxErrorException, OutException {
        Map result = tempMsgService.getTempInfo(id);
        ModelAndView mv = new ModelAndView("/share/tempMsg");
        if (result == null) {
            mv = GetModelAndView.get404Page();
        }
        mv.addObject("result", gson.toJson(result));
        return mv;
    }
}
